
package com.ifcar.app.adapter;

import com.ifcar.app.model.Comment;
import com.ifcar.app.model.MyComment;
import com.ifcar.app.rss.GetComments;
import com.ifcar.app.util.ModelUtils;
import com.ifcar.app.util.StringUtil;
import com.ifcar.app.util.ThemeSettingsHelper;
import com.ifcar.app.view.FloorsView;
import com.ifcar.app.view.FloorsView.FloorViewHolder;
import com.ifcar.app.activity.R;

import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的跟帖adatper
 * @author wjying
 *
 */
public class CommentAdapter extends ArrayAdapter<Map<String, Object>> implements
        FloorsView.FloorBinder, OnClickListener {

    static final int MSG_EXPANDS_HIDE = 0;

    LayoutInflater mLayoutInflater;

    ThemeSettingsHelper mThemeSettingsHelper;
    
    private FloorViewHolder mFloorViewHolder;

    float mTextSize;

    public CommentAdapter(Context context, List<Map<String, Object>> objects) {
        super(context, 0, objects);
        mLayoutInflater = LayoutInflater.from(context);
        mThemeSettingsHelper = ThemeSettingsHelper.getThemeSettingsHelper(context);
        mFloorViewHolder = new FloorViewHolder();
    }

    public void setTextSize(float size) {
        if (mTextSize != size) {
            mTextSize = size;
            notifyDataSetChanged();
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.comment_floor_last, null);
            FloorsView floors = (FloorsView) convertView.findViewById(R.id.sub_floors);
            floors.setFloorBinder(this);
            floors.setFloorViewHolder(mFloorViewHolder);
        }

        TextView username = (TextView) convertView.findViewById(R.id.last_floor_username);
        TextView date = (TextView) convertView.findViewById(R.id.last_floor_date);
        TextView content = (TextView) convertView.findViewById(R.id.last_floor_content);
        TextView orig = (TextView) convertView.findViewById(R.id.last_floor_orig);
        FloorsView floors = (FloorsView) convertView.findViewById(R.id.sub_floors);

        Map<String, Object> data = getItem(position);
        if (data == null) {
            data = new HashMap<String, Object>();
        }

        int last = getLastFloorNum((Map<String, Object>) data);
        if (last < 0) {
            return convertView;
        }

        // 取出最后层绑定
        Map<String, Object> map = getFloor(data, last);

        username.setText((String) ModelUtils.getValue(map, Comment.PARAM_F));
        date.setText(StringUtil.getDate((String) ModelUtils.getValue(map, Comment.PARAM_T)));
        
        
        String origStr = getOrig(data, map);
        
        orig.setText(origStr);
        content.setText((String) ModelUtils.getValue(map, Comment.PARAM_B));
        content.setTextSize(mTextSize);
        floors.setFloorsValue(data);
        floors.setFloorColor(mThemeSettingsHelper.getThemeColor(getContext(), R.color.floor).getDefaultColor());
        floors.setFloorStrokeColor(mThemeSettingsHelper.getThemeColor(getContext(), R.color.floor_stroke).getDefaultColor());
        return convertView;
    }
    
    String getOrig(Map<String, Object> data, Map<String, Object> last) {
        String pi = (String) ModelUtils.getValue(last, Comment.PARAM_PI);
        return (!TextUtils.isEmpty(pi) && pi.startsWith("PHOT") ? getContext().getText(
                R.string.comment_orig_picture).toString() : getContext().getText(
                R.string.comment_orig).toString())
                + (String) ModelUtils.getValue(last, Comment.PARAM_S);
    }

    @Override
    public void bindFloor(ViewGroup floor, int position, Object data, int floorCount) {
//        mThemeSettingsHelper.setViewBackgroud(getContext(), floor, R.drawable.comment_bk);

        int last = getLastFloorNum((Map<String, Object>) data);
        if (last <= 1) {
            floor.findViewById(R.id.hide_floor_content).setVisibility(View.GONE);
            floor.findViewById(R.id.show_floor_content).setVisibility(View.GONE);
            return;
        }

        View hideView = floor.findViewById(R.id.hide_floor_content);
        int hide = getHidePosition((Map<String, Object>) data);
        if (hide >= 0) {
            // 说明有隐藏楼层
            if (position == hide) {
                // 当前是隐藏楼层
                floor.findViewById(R.id.show_floor_content).setVisibility(View.GONE);
                hideView.setVisibility(View.VISIBLE);
                hideView.setTag(data);
                
                TextView hideText = (TextView)hideView.findViewById(R.id.hide_text);
                View hidePb = hideView.findViewById(R.id.hide_pb);
                
                boolean isExpanding = isExpanding((Map<String, Object>) data);
                if (isExpanding) {
                    hidePb.setVisibility(View.VISIBLE);
                    hideText.setText(R.string.comment_hide_expanding);
                } else {
                    hidePb.setVisibility(View.GONE);
                    hideText.setText(R.string.comment_show_hide);
                }
                
                hideText.setTextSize(mTextSize);
                return;
            } else if (position > hide) {
                // 当前是隐藏楼层后面的层
                position = position + (last - 5);
            }
        }

        hideView.setTag(null);
        hideView.setVisibility(View.GONE);
        floor.findViewById(R.id.show_floor_content).setVisibility(View.VISIBLE);
        
        TextView username = (TextView) floor.findViewById(R.id.floor_username);
        TextView num = (TextView) floor.findViewById(R.id.floor_num);
        TextView content = (TextView) floor.findViewById(R.id.floor_content);
        Map<String, Object> map = getFloor((Map<String, Object>)data, position + 1);
        username.setText((String) ModelUtils.getValue(map, Comment.PARAM_F));

        num.setText(String.valueOf(position + 1));
        content.setText((String) ModelUtils.getValue(map, Comment.PARAM_B));
        content.setTextSize(mTextSize);
    }

    @Override
    public View createFloorContentView(Object datas, ViewGroup parent) {
        View floor = mLayoutInflater.inflate(R.layout.comment_floor_normal, parent, false);
        View hideView = floor.findViewById(R.id.hide_floor_content);
        hideView.setOnClickListener(this);
        return floor;
    }

    @Override
    public int getFloorCount(Object data) {
        Map<String, Object> map = (Map<String, Object>) ModelUtils.getValue(
                (Map<String, Object>) data, MyComment.PARAM_BD);
        if (map == null || map.size() == 0) {
            return 0;
        }

        int hide = getHidePosition((Map<String, Object>) data);
        if (hide >= 0) {
            // 最后一层已经已经绑定过，还要加上隐藏楼层
            return map.size();
        }

        // 最后一层已经已经绑定过
        return map.size() - 1;
    }

    /**
     * 返回最后楼层号
     * 
     * @param data
     * @return 返回-1说明没有楼层
     */
    public int getLastFloorNum(Map<String, Object> data) {
        Map<String, Object> map = (Map<String, Object>) ModelUtils.getValue(
                (Map<String, Object>) data, MyComment.PARAM_BD);
        if (map == null) {
            return -1;
        }
        return map.size();
    }

    /**
     * 返回隐藏楼层号
     * 
     * @param data
     * @return 返回-1说明没有隐藏楼层
     */
    int getHidePosition(Map<String, Object> data) {
        int last = getLastFloorNum(data);
        if (last < 0) {
            return -1;
        }
        Map<String, Object> map = (Map<String, Object>) ModelUtils.getValue(
                (Map<String, Object>) data, MyComment.PARAM_BD);
        if (map == null || map.size() == 0) {
            return -1;
        }
        int count = map.size();

        // 当最后一层楼标识和楼层数不一样时,说明有隐藏
        if (last != count) {
            // 都返回2，这个要和后台接口参数保持一致
            return 2;
        }

        return -1;
    }
    
    public Map<String, Object> getFloor(Map<String, Object> data, int num) {
        Map<String, Object> map = (Map<String, Object>) ModelUtils.getValue(
                (Map<String, Object>) data, MyComment.PARAM_BD);
        return (Map<String, Object>) ModelUtils.getValue(map, String.valueOf(num));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.hide_floor_content:
                Map<String, Object> data = (Map<String, Object>) v.getTag();
                expandHide(data);
                break;

            default:
                break;
        }

    }

    void expandHide(Map<String, Object> data) {

    }
    
    boolean isExpanding(Map<String, Object> data) {
        Object xxx = ModelUtils.getValue(data, "expanding");
        if (xxx != null && xxx instanceof Boolean) {
            return (Boolean)xxx;
        }
        return false;
    }

    Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            CommentAdapter.this.handleMessage(msg);
        }

    };

    void handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_EXPANDS_HIDE:

                break;

            default:
                break;
        }
    }
    
}
